import { getObjProperty } from '../utils';
class BaseElement {
    points = [];
    lineWidth = 1;
    strokeStyle = '#000000';
    fillStyle = '#ffffff';
    offset = 0.5;
    zIndex = 1;
    constructor(opts) {
        const { points = [], style = {}, action = {} } = opts;
        this.points = points;
        // this.style = style;
        this.lineWidth = getObjProperty(style, 'lineWidth', 1);
        this.strokeStyle = getObjProperty(style, 'strokeStyle', '#000000');
        this.fillStyle = getObjProperty(style, 'fillStyle', '#ffffff');
        this.offset = getObjProperty(style, 'offset', 0.5);
        this.zIndex = getObjProperty(style, 'zIndex', 1);

        this.lineDash = getObjProperty(style, 'lineDash', null);
        this.lineDashOffset = getObjProperty(style, 'lineDashOffset', null);

        // "butt" "round" "square"
        this.lineCap = getObjProperty(style, 'lineCap', 'butt');
        // "bevel" "round" "miter"
        this.lineJoin = getObjProperty(style, 'lineJoin', 'round');

        this.beginPath = getObjProperty(action, 'beginPath', true);
        this.closePath = getObjProperty(action, 'closePath', false);
        this.save = getObjProperty(action, 'save', true);
        this.restore = getObjProperty(action, 'restore', true);
        this.stroke = getObjProperty(action, 'stroke', true);
        this.fill = getObjProperty(action, 'fill', false);
    }
    beforDraw(ctx) {
        let lineWidth = parseInt(this.lineWidth);
        if (this.save) {
            // console.log('save');
            ctx.save();
        }
        if (this.beginPath) {
            // console.log('beginPath');
            ctx.beginPath();
        }

        ctx.lineWidth = lineWidth;


        ctx.lineCap = this.lineCap;
        ctx.lineJoin = this.lineJoin;

        if (null !== this.lineDash) {
            ctx.setLineDash(this.lineDash);
        }
        if (null !== this.lineDashOffset) {
            ctx.lineDashOffset = this.lineDashOffset;
        }
    }
    afterDraw(ctx) {
        if (this.closePath) {
            ctx.closePath();
        }

        if (this.fill) {
            // console.log('fill');
            ctx.fillStyle = this.fillStyle;
            ctx.fill();
        }

        if (this.stroke) {
            // console.log('stroke');
            ctx.strokeStyle = this.strokeStyle;
            ctx.stroke();
        }
        if (this.restore) {
            // console.log('restore');
            ctx.restore();
        }
    }

    addPoint(points) {
        this.points = [...this.points, points];
    }

    update(opts) {
        this.points = opts.points || [];
    }
}
export default BaseElement;
